﻿#region Usings

using System;
using System.Diagnostics;
using System.Xml.Serialization;

using Gonte.Collections;
using Gonte.Reflection;
using System.Collections.Generic; 

#endregion

namespace Gonte.Logging
{
    [XmlRoot("Logging")]
    public sealed class LoggingConfiguration
    {
        #region Constants

        const string DefaultListener = "DefaultListener"; 

        #endregion

        #region Methods

        internal TraceListener GetDefaultListener()
        {
            if (!listeners_.ContainsKey(DefaultListener))
            {
                listeners_.Add(DefaultListener, new TextWriterTraceListener(@"C:\Logs.txt"));
            }
            return listeners_[DefaultListener];
        } 

        #endregion

        #region Properties

        [XmlIgnore()]
        public Dictionary<string, TraceListener> Listeners
        {
            get 
            {
                if (listeners_.Count == 0)
                {
                    foreach (ProxyObject proxyObject in tracers_.Values)
                    {
                        TraceListener listener = proxyObject.Object as TraceListener;
                        listeners_.Add(proxyObject.Name, listener);
                    }
                }

                return listeners_; 
            }

            set { listeners_ = value; }
        }

        [XmlElement("Tracer")]
        [DictionaryItemAttribute(
            KeyName = "Name",
            KeyType = typeof(string),
            ValueType = typeof(ProxyObject)
        )]
        public Dictionary<string, ProxyObject> Tracers
        {
            get { return tracers_; }
            set { tracers_ = value; }
        }

        [XmlElement("Logger")]
        [DictionaryItemAttribute(
            KeyName = "Name",
            KeyType = typeof(string),
            ValueType = typeof(Logger)
        )]
        public Dictionary<string, Logger> Loggers
        {
            get { return loggers_; }
            set { loggers_ = value; }
        } 

        #endregion

        #region Fields

        private Dictionary<string, TraceListener> listeners_ = new Dictionary<string, TraceListener>();
        private Dictionary<string, ProxyObject> tracers_ = new Dictionary<string, ProxyObject>();
        private Dictionary<string, Logger> loggers_ = new Dictionary<string, Logger>(); 

        #endregion
    }
}
